//请实现一个函数，用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样，那么它是对称的。 
//
// 例如，二叉树 [1,2,2,3,4,4,3] 是对称的。 
//
// 1 
// / \ 
// 2 2 
// / \ / \ 
//3 4 4 3 
//但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 
//
// 1 
// / \ 
// 2 2 
// \ \ 
// 3 3 
//
// 
//
// 示例 1： 
//
// 输入：root = [1,2,2,3,4,4,3]
//输出：true
// 
//
// 示例 2： 
//
// 输入：root = [1,2,2,null,3,null,3]
//输出：false 
//
// 
//
// 限制： 
//
// 0 <= 节点个数 <= 1000 
//
// 注意：本题与主站 101 题相同：https://leetcode-cn.com/problems/symmetric-tree/ 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 
// 👍 222 👎 0


package leetcode.editor.cn1;

import com.leetcode.entity.TreeNode;

import java.util.Stack;

//Java：对称的二叉树
public class DuiChengDeErChaShuLcof {
    public static void main(String[] args) {
        Solution solution = new DuiChengDeErChaShuLcof().new Solution();
        // TO TEST
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(3);

        System.out.println(solution.isSymmetric(root));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSymmetric(TreeNode root) {
            if (root == null) return true;
            if (root.left == null && root.right == null) return true;
            return isSymmetricItera(root.left, root.right);
        }

        /**
         * 递归实现
         * @param left
         * @param right
         * @return
         *      Time: O(n)  Space: 0(n)
         */
        public boolean isSymmetricRecur(TreeNode left, TreeNode right) {
            // left.val == right.val
            if (left == null && right == null) return true;
            if (left == null || right == null) return false;
            return left.val == right.val && isSymmetricRecur(left.left, right.right)
                    && isSymmetricRecur(left.right, right.left);
        }

        /**
         * 迭代实现
         * @param left
         * @param right
         * @return
         */
        public boolean isSymmetricItera(TreeNode left, TreeNode right) {
            Stack<TreeNode> stack = new Stack<>();
            stack.push(left);
            stack.push(right);

            while (!stack.isEmpty()) {
                TreeNode n1 = stack.pop();
                TreeNode n2 = stack.pop();
                if (n1 == null && n2 == null)
                    continue;
                if (n1 == null || n2 == null)
                    return false;
                if (n1.val != n2.val)
                    return false;

                // 把子节点依次入栈
                stack.push(n1.left);
                stack.push(n2.right);
                stack.push(n1.right);
                stack.push(n2.left);
            }

            // 栈为空，还没提前返回，返回true
            return true;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}
